<?php

namespace app\modules\setting\controllers\auth;

use app\common\controllers\AdminOptionsController;
use app\common\helper\CommonHelper;
use Yii;
use app\modules\setting\models\auth\EtcOfficeProductSelected;
use app\modules\setting\configs\auth\DhlbConfig;
use app\modules\setting\models\auth\EtcOfficeProductSelectedSearch;
use sookie\react\AdminController;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\base\Exception;
use sookie\excel\SaveExcel;
use sookie\db\DBHandler;
use sookie\react\handlers\ImportHandler;


/**
 * DhlbController implements the CRUD actions for EtcOfficeProductSelected model.
 */
class DhlbController extends AdminOptionsController
{
    /**
    * @return array
    * 列表数据
    */
    public function actionIndex()
    {
        try {
            $searchModel = new EtcOfficeProductSelectedSearch();
            $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
            $data = $dataProvider->getModels();
            if($data){
                $data = array_map(function($item){
                    $re = $item;
                    if(is_object($item)){
                        $re = $item->toArray();
                    }
                    $re['product_id'] = CommonHelper::getProductNameById($item['product_id']);
                    return $re;
                },$data);
            }
            return ArrayHelper::merge(["data" =>$data, "pagination" => $dataProvider->getPagination()],DhlbConfig::configIndex());
        }catch (\Throwable $e){
            return ["ok" => false,"msg" => $e->getMessage(),"code" => 401];
        }
    }






    protected function findModel($id)
    {
        if (($model = EtcOfficeProductSelected::findOne($id)) !== null) {
            return $model;
        }

        throw new Exception('不存在的记录');
    }




}

